package cube.utils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import cube.utils.comp.Order;

/**
 * 排序工具
 * 
 * @author abiao
 */
public class SortUtil {

    /**
     * 将Map按照value进行排序
     */
    public static <K, V extends Comparable<V>> List<Entry<K, V>> sortByValues(final Map<K, V> map, final Order order) {
        List<Entry<K, V>> sortedEntryList = new ArrayList<Entry<K, V>>(map.entrySet());
        Collections.sort(sortedEntryList, new Comparator<Entry<K, V>>() {
            public int compare(Entry<K, V> o1, Entry<K, V> o2) {
                if (order == Order.ASC) {
                    return o1.getValue().compareTo(o2.getValue());
                } else {
                    return o2.getValue().compareTo(o1.getValue());
                }
            }
        });
        return sortedEntryList;
    }
}
